#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
#define rep(i, a, b) for (int i = (a); i <= (b); i++)
#define per(i, a, b) for (int i = (a); i >= (b); i--)
#define endl '\n'
const int N = 3005;

int n, k;
int a[N], l[N], r[N], d[N];
void solve() {
  cin >> n >> k;
  rep(i, 1, n) cin >> a[i];
  int ans = 1e9;
  rep(i, 1, n) {
    rep(j, 1, k) l[j] = r[j] = n + 1;
    per(j, i, 1) l[a[j]] = min(l[a[j]], i - j);
    rep(j, i, n) r[a[j]] = min(r[a[j]], j - i);
    int sum = 0;
    rep(j, 1, k) sum += l[j], d[j] = r[j] - l[j];
    sort(d + 1, d + k + 1);
    rep(j, 1, (k + 1) / 2) sum += d[j];
    ans = min(ans, sum);
  }
  rep(i, 1, k) ans -= abs(i - (k + 1) / 2);
  cout << ans << endl;
}

int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  int t;
  cin >> t;
  while (t--) solve();
  return 0;
}